package com.itheima.reggie.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Override
    public void deleteCategory(Long id) {
        //1.根据分类ID查询该分类下是否有菜品,如果有,则不再继续往下执行,并提示错误信息
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Dish::getCategoryId,id);
        Integer disCount = dishMapper.selectCount(dishLambdaQueryWrapper);

        if (disCount>0){
            throw new CustomException("该分类下有关关联菜品,不能直接删除!");
        }

        //2.根据分类ID查询该分类下是否有套餐,如果有,则不再继续往下执行,并提示错误信息
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,id);
        Integer setmealCount = setmealMapper.selectCount(setmealLambdaQueryWrapper);

        if (setmealCount>0){
            throw new CustomException("该分类下有关联套餐信息,不能直接删除!");
        }

        //3.删除分类
        this.removeById(id);
    }
}
